package org.darkforest.common.services.impl;

import org.darkforest.common.services.GoodsTagService;
import org.darkforest.dao.mapper.GoodsTagMapper;
import org.darkforest.dao.mapper.TagFestivalRelationMapper;
import org.darkforest.dao.mapper.TagRelationRelationMapper;
import org.darkforest.model.GoodsTag;
import org.darkforest.model.TagFestivalRelation;
import org.darkforest.model.TagRelationRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */
@Service
public class GoodsTagServiceImpl implements GoodsTagService {

    @Autowired
    private TagFestivalRelationMapper tagFestivalRelationMapper;

    @Autowired
    private TagRelationRelationMapper tagRelationRelationMapper;


    @Autowired
    private GoodsTagMapper goodsTagMapper;


    /**
     * 新增商品标签 并且建立与节日 关系 的关联
     *
     * @param goodsTag
     * @param fIds
     * @param rIds
     * @return
     */

    @Transactional
    public boolean addGoodsTag(GoodsTag goodsTag, Integer[] fIds, Integer[] rIds) {


        goodsTag.setTagState(goodsTag.getTagState());
        goodsTag.setTagType("1");


        goodsTagMapper.insertSelective(goodsTag);
        if (fIds!= null) {
            for (Integer fId : fIds) {
                TagFestivalRelation tagFestivalRelation = new TagFestivalRelation(goodsTag.getTagId(), fId);
                tagFestivalRelationMapper.insertSelective(tagFestivalRelation);
            }

        }
        if (rIds!= null) {
            for (Integer rId : rIds) {
                TagRelationRelation tagRelationRelation = new TagRelationRelation(goodsTag.getTagId(), rId);
                tagRelationRelationMapper.insertSelective(tagRelationRelation);
            }
        }

        return true;


    }

    /**
     * 批量删除商品标签
     *
     * @param tagIds
     * @return
     */
    @Transactional
    public boolean batchDeleteGoodsTags(Integer[] tagIds) {
        if(tagIds!=null){
            for (Integer tagId : tagIds) {
                TagFestivalRelation tagFestivalRelation = new TagFestivalRelation();
                tagFestivalRelation.setTagId(tagId);
                tagFestivalRelationMapper.deleteByCondition(tagFestivalRelation);

                TagRelationRelation tagRelationRelation = new TagRelationRelation();
                tagRelationRelation.setTagId(tagId);
                tagRelationRelationMapper.deleteByCondition(tagRelationRelation);


                goodsTagMapper.deleteByPrimaryKey(tagId);
            }
        }



        return true;
    }

    /**
     * 编辑商品标签以及与节日 关系的关联
     *
     * @param goodsTag
     * @param fIds
     * @param rIds
     * @return
     */
    @Transactional
    public boolean updateGoodsTag(GoodsTag goodsTag, Integer[] fIds, Integer[] rIds) {

        goodsTagMapper.updateByPrimaryKeySelective(goodsTag);

        TagFestivalRelation tagFestivalRelation = new TagFestivalRelation();
        tagFestivalRelation.setTagId(goodsTag.getTagId());
        tagFestivalRelationMapper.deleteByCondition(tagFestivalRelation);

        TagRelationRelation tagRelationRelation = new TagRelationRelation();
        tagRelationRelation.setTagId(goodsTag.getTagId());
        tagRelationRelationMapper.deleteByCondition(tagRelationRelation);

        if(fIds!=null){
            for (Integer fId : fIds) {
                TagFestivalRelation tf = new TagFestivalRelation(goodsTag.getTagId(), fId);
                tagFestivalRelationMapper.insertSelective(tf);
            }
        }


        if(rIds!=null){
            for (Integer rId : rIds) {
                TagRelationRelation tr = new TagRelationRelation(goodsTag.getTagId(), rId);
                tagRelationRelationMapper.insertSelective(tr);
            }
        }


        return true;
    }

    @Override
    public List<GoodsTag> selectListByCondition(GoodsTag goodsTag) {

        List<GoodsTag> goodsTags =  goodsTagMapper.selectGoodsTagByCondition(goodsTag);
        for(GoodsTag goodsTag1:goodsTags){
            goodsTag1.setFestivalList(tagFestivalRelationMapper.selectByTagId(goodsTag1.getTagId()));
            goodsTag1.setRelationList(tagRelationRelationMapper.selectByTagId(goodsTag1.getTagId()));
        }
        return goodsTags;
    }
}
